<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2017/11/9  19:27
 */
namespace app\pc\logic;
use app\pc\BaseModel;

class Index extends  BaseModel{

	//检测参数是否已传
    protected $ParameterDetection  = [
    	'banner'=>['type']
    ];

	/**
	* 首页
	*/
	public function index(){
		$result = [];

		$nav_category = $this->article('首页');

		$result['article'] = $nav_category;

		$result['adviser'] = $this->look_consultant();

		return $result;
	}

	/**
	*  找贷款
	*/	
	public function looking_loan(){
		$result = [];

		$nav_category = $this->article('找贷款');

		$result['article'] = $nav_category;

		$result['adviser'] = $this->look_consultant();

		return $result;
	}

	/**
	*  找顾问
	*/
	public function find_consultant(){
		$result = [];
		$pagesize = '10';
		$page = isset($this->requestData['page'])?$this->requestData['page']:1;

		$adviser = db('adviser')->order('sort desc')->page($page,$pagesize)->select();
		$numrows = db('adviser')->count();

		$pages=ceil($numrows/$pagesize);

		foreach ($adviser as $key => &$value) {
			$value['head_img'] = IMG_PATH.$value['head_img'];
		}

		$result['adviser'] = $adviser;
		$result['total_pages'] = $pages;
		return $result;
	}

	//轮播
	public function banner()
	{
		$type = $this->requestData['type'];
		$type = explode(',', $type);
		$data = db('banner')->where(['status'=>'1','type'=>['in',$type]])->order('sort desc')->select();
		foreach($data as $key => $value){
			$data[$key]['img'] = IMG_PATH.$data[$key]['img'];
		}
		return $data;
	}

	//文章查询
	public function article($name){
		$nid = $this->articleNew->get_nav_id($name);
		$nav_category = $this->articleNew->get_nav_category($nid,0);

		foreach ($nav_category as $key => $value) {
			$article = db('article')->where(['cid'=>$value['id']])->order('sort desc')->select();
			foreach ($article as $kk => &$vv) {
				$vv['image'] = IMG_PATH.$vv['image'];
			}
			$nav_category[$key]['article_list'] = $article;
		}

		return $nav_category;
	}

	//找顾问
	public function look_consultant(){
		$adviser = db('adviser')->order('sort desc')->select();
		foreach ($adviser as $key => &$value) {
			$value['head_img'] = IMG_PATH.$value['head_img'];
		}
		return $adviser;
	}
}